package cn.iocoder.yudao.module.game.service.otherGame;

import cn.iocoder.yudao.module.game.controller.admin.gameToken.vo.GameTokenSaveReqVO;
import cn.iocoder.yudao.module.game.dal.dataobject.gameToken.GameTokenDO;
import cn.iocoder.yudao.module.game.service.gameToken.GameTokenService;
import cn.iocoder.yudao.module.game.utils.AESEncrypt;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import net.sf.json.JSONObject;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.net.URLEncoder;
import java.util.Map;

/**
 * 游戏列表 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class JPGServiceImpl implements JPGService {

    // 测试环境
//    String token = "8e9d86e73d8b42934a9fb35e93f77d23";
//    String secret = "eb47798feab8a1502a8b091e3cbfda5a";
//    String url = "https://api.vintebet.com";

    // 正式环境
    String token = "11db4a10d4a33a86fe36cd26f1f25bee";
    String secret = "663e2d03d652974a971ca80f7ff71f4f";
    String url = "https://api.mines25.com";

    @Resource
    private GameTokenService gameTokenService;

    private String sign(JSONObject data) {
        String encodedJson = "";
        for (Object key : data.keySet()) {
            System.out.println(key.toString() + "=" + data.getString(key.toString()));
            encodedJson += key.toString() + "=" + data.getString(key.toString()) + "&";
        }
        System.out.println("secret:  " + secret);
        encodedJson += "key=" + secret;
        System.out.println("encodedJson:  " + encodedJson);

        String sign = DigestUtils.md5Hex(encodedJson);
        System.out.println("sign:  " + sign);

        return sign;
    }

    @Override
    public JSONObject login(Map<String, Object> params) throws Exception {
        System.out.println("token:  " + token);

        JSONObject data = new JSONObject();
        data.put("operator_token", token);
        data.put("ts", System.currentTimeMillis());
        data.put("user_id", params.get("userId"));
        data.put("user_name", params.get("mobile"));
        data.put("sign", sign(data));

        String action = "/api/web/user_session/";

        CloseableHttpClient client = HttpClientBuilder.create().build();

        StringEntity stringEntity = new StringEntity(data.toString());
        System.out.println(url + action);
        HttpPost httpPost = new HttpPost(url + action);
        httpPost.setEntity(stringEntity);
        // gzip
        // httpPost.setHeader("Accept-Encoding", "gzip");
        HttpEntity httpEntity = null;
        String res = "{}";
        try {
            // Resolve response
            HttpResponse response = client.execute(httpPost);
            httpEntity = response.getEntity();
            res = EntityUtils.toString(httpEntity);
            System.out.println("POST:  " + res);
        } finally {
            EntityUtils.consume(httpEntity);
        }

        return JSONObject.fromObject(res);
    }

    @Override
    public JSONObject getGame(Map<String, Object> params) throws Exception {
        System.out.println("token:  " + token);

        JSONObject data = new JSONObject();
        data.put("game_code", params.get("gameId"));
        data.put("operator_token", token);
        data.put("ts", System.currentTimeMillis());
        data.put("user_id", params.get("userId"));
        data.put("user_token", params.get("userToken"));
        data.put("sign", sign(data));

        String action = "/api/web/game_url/";

        CloseableHttpClient client = HttpClientBuilder.create().build();

        StringEntity stringEntity = new StringEntity(data.toString());
        System.out.println(url + action);
        HttpPost httpPost = new HttpPost(url + action);
        httpPost.setEntity(stringEntity);
        // gzip
        // httpPost.setHeader("Accept-Encoding", "gzip");
        HttpEntity httpEntity = null;
        String res = "{}";
        try {
            // Resolve response
            HttpResponse response = client.execute(httpPost);
            httpEntity = response.getEntity();
            res = EntityUtils.toString(httpEntity);
            System.out.println("POST:  " + res);
        } finally {
            EntityUtils.consume(httpEntity);
        }

        return JSONObject.fromObject(res);
    }

}